<template>
  <el-dialog
    :title="form.id?'编辑权限':'新增权限'"
    :visible="dialogVisible"
    width="30%"
    @close="closeFn"
  >
    <el-form ref="form" :rules="rules" :model="form" label-width="80px">
      <el-form-item prop="title" label="权限名称">
        <el-input v-model="form.title" />
      </el-form-item>
      <el-form-item prop="code" label="权限标识">
        <el-input v-model="form.code" />
      </el-form-item>
      <el-form-item label="权限描述">
        <el-input v-model="form.description" />
      </el-form-item>
      <el-form-item label="开启">
        <el-switch
          v-model="form.enVisible"
          active-value="1"
          inactive-value="0"
        />
      </el-form-item>
    </el-form>
    <template slot="footer" class="dialog-footer">
      <el-row type="flex" justify="center">
        <el-button @click="closeFn">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { addPermission, perPermission, editPermission } from '@/api/authority'
export default {
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      form: {
        title: '',
        code: '',
        description: '',
        enVisible: '1', // 开启1   关闭0
        pid: '', // 父级权限的id
        type: '' // 权限类型 1 表示一级权限 2 表示二级权限
      },
      rules: {
        title: [
          { required: true, message: '请输入权限名称', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入权限标识', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    closeFn() {
      this.$emit('update:dialogVisible', false)
      this.$refs.form.resetFields()
      this.form.description = ''
    },
    async submit() {
      await this.$refs.form.validate()
      if (this.form.id) {
        await editPermission(this.form)
      } else {
        // console.log(75, this.form)
        await addPermission(this.form)
      }
      this.$emit('updateEvent')
      this.closeFn()
    },
    // 编辑 回显详情
    async  getPerPermission(id) {
      this.form = await perPermission(id)
    }
  }
}
</script>
